$(function () {
    let username_input = $('#username')
    let email_input = $('#email')
    let register = $('#register')
    // 异步检测注册账号是否已经存在，如果存在了则在页面上用红色字体显示出来，不存在则用绿色显示出来
    username_input.change(function () {
        let username = username_input.val().trim()
        if (username.length >= 6) {
            $.getJSON('/app/checkUser', {'username': username}, function (response) {
                let check_tips = $('#username_check_tips')
                checkExist(response, check_tips)
                check_tips.text(response["msg"])
            })
        }
    })
    // 异步检测注册邮箱是否已经存在，如果存在了则在页面上用红色字体显示出来，不存在则用绿色显示出来
    email_input.change(function () {
        let email = email_input.val().trim()
        if (email.length) {
            $.getJSON('/app/checkEmail', {"email": email}, function (response) {
                let email_tips = $('#email_check_tips')
                checkExist(response, email_tips)
                email_tips.text(response["msg"])
            })
        }
    })
})

// 封装处理响应样式函数
function checkExist(response, tips) {
    if (response["static"] === 0) {
        tips.css({"color": "green"})
    } else {
        tips.css({"color": "red"})
    }
}

// 提交表单处理函数
function checkRegister() {
    let $password = $('#password')
    let $confirm_password = $('#confirm_password')
    let password = $password.val().trim()
    let confirm_password = $confirm_password.val().trim()
    $password.val(hex_md5(password))
    $confirm_password.val(hex_md5(confirm_password))
    return true
}